package com.macken.wowoadmin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.macken.wowadmin.dao.AdminDao;
import com.macken.wowadmin.dao.HostDao;
import com.macken.wowadmin.entity.AdminInfo;
import com.macken.wowadmin.entity.HostInfo;
import com.macken.wowadmin.util.SoapUtil;


@Controller
@RequestMapping("/console/")
public class ConsoleController {

	HostDao hostDao = HostDao.instance;
	AdminDao adminDao=AdminDao.instance;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request) {
		List<HostInfo> hosts=hostDao.getHostList();
		request.setAttribute("hosts",hosts);
		return "/console/index.jsp";
	}


	@RequestMapping("command.do")
	public void command(HttpServletRequest request, HttpServletResponse response, @RequestParam String cmd,@RequestParam int hostid)
			throws Exception {
		HostInfo host=hostDao.getHostById(hostid);
		AdminInfo admin=adminDao.getAdminByHostId(host.getHostId());
		Gson gson = new Gson();
		Map<String, String> map = new HashMap<String, String>();
		String res = SoapUtil.command(cmd.trim(),host.getIp(),host.getPort(),admin.getAdminName(),admin.getAdminPass());
		map.put("data", res);
		response.getWriter().write(gson.toJson(map));
		return;
	}
}
